from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
# 1.配置
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:mysql@127.0.0.1:3306/toutiao'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ECHO'] = True

# 2.创建sqlalchemy对象,并关联app
# 方式1:
# db = SQLAlchemy(app)
# 方式2:
db = SQLAlchemy()
db.init_app(app)

# 3.定义模型类
class User(db.Model):
    """
    用户基本信息
    """
    # 使用__tablename__指定需要关联的表名称
    # 如果没有指定,就是user.UserBasic --> user_basic
    __tablename__ = 'user_basic'

    # 此类是业务自已用的,跟sqlalchemy没关系
    class STATUS:
        ENABLE = 1
        DISABLE = 0

    # 参数1:指定字段名,如果不指定,跟属性名一致
    # 参数2:数据类型
    # 参数3:列选项
    id = db.Column('user_id', db.Integer, primary_key=True, doc='用户ID')
    mobile = db.Column(db.String, doc='手机号')
    password = db.Column(db.String, doc='密码')
    name = db.Column('user_name', db.String, doc='昵称')
    profile_photo = db.Column(db.String, doc='头像')
    last_login = db.Column(db.DateTime, doc='最后登录时间')
    is_media = db.Column(db.Boolean, default=False, doc='是否是自媒体')
    is_verified = db.Column(db.Boolean, default=False, doc='是否实名认证')
    introduction = db.Column(db.String, doc='简介')
    certificate = db.Column(db.String, doc='认证')
    article_count = db.Column(db.Integer, default=0, doc='发帖数')
    following_count = db.Column(db.Integer, default=0, doc='关注的人数')
    fans_count = db.Column(db.Integer, default=0, doc='被关注的人数（粉丝数）')
    like_count = db.Column(db.Integer, default=0, doc='累计点赞人数')
    read_count = db.Column(db.Integer, default=0, doc='累计阅读人数')

    account = db.Column(db.String, doc='账号')
    email = db.Column(db.String, doc='邮箱')
    status = db.Column(db.Integer, default=1, doc='状态，是否可用')

if __name__ == '__main__':
    # 4.调用sqlalchemy提高的算法,操作mysql
    with app.app_context():
        users = User.query.all()
        print(users)

